{include file="common/header"/}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md2 layui-col-sm2 layui-col-xs12">
            <div class="layui-card">
              <div class="layui-card-header">栏目列表</div>
              <div class="layui-card-body">
                <div class="layui-row layui-form">
                    <div id="test12" class="demo-tree-more"></div>
                </div>
              </div>
            </div>
        </div>
        <div class="layui-col-md10 layui-col-sm10 layui-col-xs12">
            <div class="layui-card">
              <div class="layui-card-body">
                <div class="layui-row">
                    <form action="" class="layui-form">
                        <div class="layui-row layui-col-space10">
                          <div class="layui-col-md2 layui-col-xs4">
                            <input type="text" name="" id="title" lay-verify="required" placeholder="标题" class="layui-input">
                          </div>
                          <div class="layui-col-md2 layui-col-xs4">
                            <input type="button" id="searchBtn" name="submit" class="layui-btn" value="搜索">
                          </div>
                        </div>
                    </form>
                </div>
                <div class="layui-row">
                    <table class="layui-table" id="list" lay-filter="list"></table>
                </div>
              </div>
            </div>
        </div>
    </div>
</div>
{include file="common/footer"/}

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <a href="javascript:;" class="layui-btn layui-btn-sm" onclick="xadmin.open('{:lang('add')}','{:url("add")}');"><i class="layui-icon layui-icon-add-circle"></i>{:lang('add')}</a>
        <a href="javascript:;" data-href="{:url('del')}" class="layui-btn layui-btn-sm layui-btn-danger" id="delAll">批量删除</a>
    </div>
    
</script>


<script type="text/html" id="action">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-xs" onclick="xadmin.open('{:lang('edit')}','{:url("edit")}?id={{d.id}}');">{:lang('edit')}</a>
        <a data-href="{:url('del')}" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
    </div>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" data-href="{:url('state')}" value="{{d.id}}" lay-skin="switch" lay-text="正常|隐藏" lay-filter="status" {{ d.status == '1' ? 'checked' : '' }}>
</script>

<script type="text/html" id="titleTpl">
    {{d.title}}<img src="/static/admin/images/image.gif" onmouseover="layer.tips('<img src={{d.image}}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();">
</script>
<script>
var searchObj = {};
var catids = [];
layui.use(['table','laydate','tree'],function(){
  var table = layui.table;
  var tree = layui.tree;
    tree.render({
        elem: '#test12'
        ,data: {$treedata|raw}
        ,showCheckbox: true
        ,onlyIconControl: true
        ,id: 'demoId'
        ,oncheck: function(obj){
            var checkData = tree.getChecked('demoId');
            catids = get_child(checkData);
            searchObj['catids'] = catids.length?catids:[];
            tableRender(searchObj,table);
        }
    });
    tableRender(searchObj,table);
    // 搜索
    $('#searchBtn').click(function(){
        searchObj['title'] = $('#title').val();
        table.reload('list',{where:searchObj});
    }) 
});


function get_child(data){
    if (data.length>0) {
        for (var i = 0; i < data.length; i++) {
            catids.push(data[i]['id']);
            if(data[i].children.length > 0){
                get_child(data[i].children);
            }
        }
    }else{
        catids = [];
    }
    
    return catids;
}

function tableRender(searchObj,table){
    table.render({
        elem: '#list'
        ,url:'{:url("index")}'
        ,method:'post'
        ,where:searchObj
        ,toolbar: '#toolbarDemo'
        ,defaultToolbar: []
        ,cellMinWidth: 60
        ,limit:20
        ,limits:[20,40,60,80,100]
        ,title: '文章列表'
        ,id:'list'
        ,cols: [[
            {type:'checkbox',fixed: true, width:80}
            ,{field:'id', title: 'ID',fixed: true}
            ,{title: '栏目',templet:function(d){
                    return d.cate?d.cate.title:'';
                }}
            ,{field:'title', title: '标题', templet:'#titleTpl'}
            ,{field:'views', title: '浏览'}
            ,{field:'status', title: '状态', templet:'#status'}
            ,{title:'操作',toolbar: '#action',align:"center"}
        ]]
        ,page: true
        ,done:function(res, curr, count){
            catids = [];
            searchObj = {};

        }
    });
}
</script>
